#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    canMove = 0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 * @brief MainWindow::mousePressEvent
 * @param event
 *
 * 只有在label的范围内按下鼠标才有用
 */
void MainWindow::mousePressEvent(QMouseEvent *event)
{

//    QRect lrect = ui->label->frameRect();

    if(event->button() == Qt::LeftButton){
         QRect lrect = ui->label->frameRect();
         lrect.translate(ui->label->pos());

         if(lrect.contains(event->pos())){

             canMove = 1;
             pos = ui->label->pos() - event->pos();
         }


    }



}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        canMove = 0;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(canMove == 1){
        QPoint nPos = pos + event->pos();

        ui->label->move(nPos.x(),nPos.y());
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    int x = ui->label->pos().x();
    int y = ui->label->pos().y();

    if(event->key() == Qt::Key_Up){
        y = y -10;

        ui->label->move(x,y);
    }else if(event->key() == Qt::Key_Down){
        y = y + 10;
        ui->label->move(x,y);
    }else if(event->key() == Qt::Key_Left){
        x = x -10;
        ui->label->move(x,y);
    }else if(event->key() == Qt::Key_Right){
        x = x + 10;
        ui->label->move(x,y);
    }
}

